#include "CString.hpp"

CString::CString(const char *str) : m_str(new char[strlen(str) + 1])
{
    strcpy(m_str, str);
}

CString::CString(const CString &str) : m_str(new char[str.size() + 1])
{
    strcpy(m_str, str.m_str);
}

CString::CString(CString &&str) noexcept : m_str(str.m_str)
{
    str.m_str = nullptr;
}

CString::~CString()
{
    delete[] m_str;
}

CString &CString::operator=(const CString &str)
{
    if (this != &str) {
        auto *new_str = new char[str.size() + 1];
        strcpy(new_str, str.m_str);
        delete[] m_str;
        m_str = new_str;
    }
    return *this;
}

CString &CString::operator=(CString &&str) noexcept
{
    if (this != &str) {
        delete[] m_str;
        m_str = str.m_str;
        str.m_str = nullptr;
    }
    return *this;
}

CString CString::operator+(const CString &str)
{
    auto *new_str = new char[size() + str.size() + 1];
    strcpy(new_str, m_str);
    strcat(new_str, str.m_str);
    return {new_str};
}

uint32_t CString::size() const
{
    return strlen(m_str);
}

char *CString::c_str() const
{
    return m_str;
}
